package net.lcheng.sellerCenter.controller;

import net.lcheng.model.BaseAreas;
import net.lcheng.model.User;
import net.lcheng.model.WxShop;
import net.lcheng.service.BaseAreasService;
import net.lcheng.service.UserService;
import net.lcheng.service.WxShopService;
import net.lcheng.vo.WxShopModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

/**
 * Created by zwr on 2016/7/4 0004.
 */
@Controller
public class ShopSettingsController {
    @Autowired
    private UserService userService;
    @Autowired
    private WxShopService wxShopService;
    @Autowired
    private BaseAreasService baseAreasService;
    /**店铺设置Get*/
    @RequestMapping(value = "shop_settings",method = RequestMethod.GET)
    public  String shopSettingsGet(Model model){
        Subject currentUser= SecurityUtils.getSubject();
        if(currentUser.isAuthenticated()){
            String userPhoneNum=currentUser.getSession().getAttribute("UserPhoneNum").toString();
            //获取用户信息
            User user=userService.getUserByPhone(userPhoneNum);
            WxShop shop=wxShopService.getShopInforByUserId(user.getUserId());
            //判断有没有店铺
            if (shop==null){
                model.addAttribute("userId",1);
            }else{
                model.addAttribute("shop",shop);
            }
        }
        model.addAttribute("WxShopModel", new WxShopModel());
        return "shop_settings";
    }
    /**店铺设置Post*/
   @RequestMapping(value = "shop_settings",method = RequestMethod.POST)
    public  String shopSettingsPost(Model model, @Valid @ModelAttribute("WxShopModel") WxShopModel wxShopModel, BindingResult result){
    if(result.hasErrors()){
        return "shop_settings";
    }
    /**获取省名称*/
    BaseAreas provinceCode=baseAreasService.getAreaCodeByAreaName(wxShopModel.getAddrProvinceCode());
    /**获取市名称*/
    BaseAreas cityCode=baseAreasService.getAreaCodeByAreaName(wxShopModel.getAddrCityCode());
       //店铺设置
     WxShop wxShop =new WxShop();
    wxShop.setShopId(wxShopModel.getShopId());
    wxShop.setShopLogo(wxShopModel.getShopLogo());
    wxShop.setShopName(wxShop.getShopName());
    wxShop.setAddrProvinceCode(wxShopModel.getAddrProvinceCode());
    wxShop.setAddrProvinceName(provinceCode.getAreaName());
    wxShop.setAddrCityCode(wxShopModel.getAddrCityCode());
    wxShop.setAddrCityName(cityCode.getAreaName());
    wxShop.setShopServiceTel(wxShopModel.getShopServiceTel());
    wxShop.setShopServiceQq(wxShopModel.getShopServiceQq());
    wxShop.setShopServiceWx(wxShopModel.getShopServiceWx());
    wxShop.setShopSummary(wxShopModel.getShopSummary());
    int returnResult = wxShopService.update(wxShop);
    if(returnResult==1){
        model.addAttribute("result",1);
    }
    return "shop_settings";
}

}
